<!DOCTYPE HTML>
<html>
<head>
<title>ObjAddRef() / ObjRelease() | AutoHotkey</title>
<meta name="description" content="The ObjAddRef and ObjRelease functions increment or decrement an object's reference count." />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>


<h1>ObjAddRef() / ObjRelease() <span class="ver">[AHK_L 53+]</span></h1>

<p>增加或减少对象的<a href="../Objects.htm#Reference_Counting">引用计数</a>.</p>

<pre class="Syntax"><span class="func">ObjAddRef</span>(Ptr)
<span class="func">ObjRelease</span>(Ptr)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Ptr</dt>
  <dd><p>非托管对象指针或 COM 接口指针.</p></dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>这两个函数返回新的引用计数. 这个值应该 <b>仅</b> 用于调试的目的.</p>

<h2 id="Related">相关</h2>
<p><a href="../Objects.htm#Reference_Counting">引用计数</a></p>
<p>尽管后面的文章讨论了适用于 COM 的引用计数, 但其中也包含了通常也适用于 AutoHotkey 对象的重要概念和规则: <a href="http://msdn.microsoft.com/en-us/library/ms691379.aspx">IUnknown::AddRef</a>, <a href="http://msdn.microsoft.com/en-us/library/ms682317.aspx">IUnknown::Release</a>, <a href="https://msdn.microsoft.com/en-us/library/ms692481.aspx">引用计数规则</a>.</p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a class="ex_number" href="#ExBasic"></a> 检索对象的指针, 并增加引用次数. 有关详情, 请参阅<a href="../Objects.htm#Implementation_Pointers">对象的指针</a>.</p>
<pre>obj := Object()

<em>; 下面的两行是等同的:</em>
ptr1 := Object(obj)
ptr2 := ObjectToPointer(obj)

ObjectToPointer(obj) {
    if !IsObject(obj)
        return ""
    ptr := &amp;obj
    ObjAddRef(ptr)
    return ptr
}

<em>; 由 Object() 或 ObjectToPointer() 返回的指针必须进行手动释放,
; 这样才能最终释放对象并回收它占用的内存.</em>
ObjRelease(ptr2)
ObjRelease(ptr1)
</pre>
</div>

<p>另一个例子, 请参阅 <a href="ComObjConnect.htm#ExIE">ComObjConnect()</a>.</p>
</body>
</html>